<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2018-04-04 10:54:25
 */
if (!defined('IN_DISCUZ')) {
    exit('Access Denid');
}
class table_jamesonread_fx extends discuz_table
{

    public function __construct()
    {
        $this->_table         = 'jamesonread_fx';
        $this->_pk            = 'id';
        $this->_pre_cache_key = 'jamesonreadfx_';
        $this->_cache_ttl     = 300;
        parent::__construct();
    }
    // 获取当前用户的分享记录数，以tid分组统计
    public function count_byuid($uid)
    {
        if ($cache = $this->fetch_cache('uid_' . $uid)) {
            return $cache;
        }
        $total = (int) DB::result_first('SELECT count(id) FROM %t WHERE fxuid=%d GROUP BY tid', array($this->_table, $uid));
        $this->store_cache('byuid_' . $uid, $total);
        return $total;
    }
    // 当前用户的分享记录和奖励,以tid分组
    public function fetch_byuid($uid, $start, $size)
    {
        $cacheid = 'fetch_byuid-'.$uid.'-'.$start.'-'.$size;
        if ($cache = $this->fetch_cache($cacheid)) {
            return $cache;
        }
        $data = (array) DB::fetch_all('SELECT SUM(f.jiangli) as jianglinums,COUNT(f.fxuid) as usernums,f.id,f.fxuid,f.addtime,f.tid,f.jiangli,f.book_id,b.book_name,b.image,b.author,t.subject FROM %t as f INNER JOIN %t as t ON f.tid=t.tid INNER JOIN %t as b ON f.book_id=b.book_id WHERE f.fxuid=%d GROUP BY f.tid ORDER BY usernums DESC LIMIT %d,%d', array($this->_table, 'jamesonread_colums', 'jamesonread_books', $uid, $start, $size));
        $this->store_cache($cacheid, $data);
        return $data;
    }
    // 后台以会员查看，合并tid，总数
    public function count_all_uid()
    {
        if ($cache = $this->fetch_cache('alluid')) {
            return $cache;
        }
        $total = (int) count(DB::fetch_all('SELECT COUNT(fxuid) FROM %t GROUP BY fxuid', array($this->_table)));
        $this->store_cache('alluid', $total);
        return $total;
    }
    // 后台以会员会员查看
    public function fetch_all_uid($start, $size)
    {
        $cacheid = 'fetch_all_uid-'.$start.'-'.$size;
        if ($cache = $this->fetch_cache($cacheid)) {
            return $cache;
        }
        $data = (array) DB::fetch_all(
            'SELECT SUM(f.jiangli) as jianglinums,COUNT(f.id) as usernums,f.fxuid,c.username FROM %t as f INNER JOIN %t as c ON f.fxuid=c.uid  GROUP BY fxuid ORDER BY usernums DESC LIMIT %d,%d', array($this->_table, 'common_member', $start, $size)
        );
        $this->store_cache($cacheid, $data);
        return $data;
    }
    // 后台以tid查看，合并uid，总数
    public function count_all_tid()
    {
        if ($cache = $this->fetch_cache('counttid')) {
            return $cache;
        }
        $total = (int) count(DB::fetch_all('SELECT COUNT(tid) FROM %t GROUP BY tid', array($this->_table)));
        $this->store_cache('counttid', $total);
        return $total;
    }
    // 后台以tid查看
    public function fetch_all_tid($start, $size)
    {
        $cacheid = 'fetch_all_tid-'.$start.'-'.$size;
        if ($cache = $this->fetch_cache($cacheid)) {
            return $cache;
        }
        $data = (array) DB::fetch_all(
            'SELECT SUM(f.jiangli) as jianglinums,COUNT(f.id) as usernums,f.tid,t.subject,b.book_name FROM %t as f INNER JOIN %t as t ON f.tid=t.tid  INNER JOIN %t as b ON f.book_id=b.book_id  GROUP BY f.tid ORDER BY usernums DESC LIMIT %d,%d', array($this->_table, 'jamesonread_colums', 'jamesonread_books', $start, $size)
        );
        $this->store_cache($cacheid, $data);
        return $data;
    }
    // 清空所有记录
    public function clear()
    {
        $this->truncate();
    }
}